/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Global Trust Authority is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

use plugin_manager::{PluginBase, ServicePlugin, ServiceHostFunctions, PluginError};
use serde_json::Value;
use async_trait::async_trait;
use std::error::Error;

/// A mock test attester plugin for testing evidence verification
pub struct MockTestAttesterPlugin {
    plugin_type: String,
    host_functions: ServiceHostFunctions,
}

impl MockTestAttesterPlugin {
    fn new(plugin_type: String, host_functions: ServiceHostFunctions) -> Self {
        Self {
            plugin_type,
            host_functions,
        }
    }
}

impl PluginBase for MockTestAttesterPlugin {
    fn plugin_type(&self) -> &str {
        self.plugin_type.as_str()
    }
}

#[async_trait]
impl ServicePlugin for MockTestAttesterPlugin {
    fn get_sample_output(&self) -> Value {
        serde_json::json!({
            "verification_result": true,
            "evidence_data": "test_evidence",
            "status": "verified"
        })
    }
    
    async fn verify_evidence(
        &self,
        _user_id: &str,
        _node_id: Option<&str>,
        _evidence: &Value,
        _nonce: Option<&[u8]>,
    ) -> Result<Value, PluginError> {
        // Simulate successful evidence verification
        Ok(serde_json::json!({
            "verification_result": true,
            "evidence_data": "test_evidence",
            "status": "verified",
            "message": "Evidence verification successful"
        }))
    }
}

#[no_mangle]
pub fn create_plugin(host_functions: ServiceHostFunctions, plugin_type: &str) -> Result<Box<dyn ServicePlugin>, Box<dyn Error>> {
    if plugin_type != "test_attester" {
        return Err(Box::new(PluginError::InputError("Invalid plugin type".to_string())));
    }
    Ok(Box::new(MockTestAttesterPlugin::new(plugin_type.to_string(), host_functions)))
}